package com.nondo.dean.platform.controller.api.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.tool.BeanUtils;
import com.nondo.dean.common.tool.ValidateUtil;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import com.nondo.dean.platform.domain.Coupon;
import com.nondo.dean.platform.service.CouponService;

/**
 * 优惠券管理后台接口
 * @author zlj
 * @date 2018年3月27日
 */
@RestController("apiAdminCoupon")
@RequestMapping("/{v}/admin/coupon")
public class CouponController {
    
    @Autowired
    CouponService couponService;
    
    /**
     * 查看优惠券详情
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/info/{id}")
    public View<Coupon> info(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View<Coupon> view = new View<Coupon>(20000, "获取数据成功");
        
        Coupon coupon = null;
        if (id != null) {
            coupon = couponService.selectCoupon(id);
        } else {
            coupon = new Coupon();
        }

        view.setData(coupon);
        return view;
    }
    
    /**
     * 添加优惠券
     * @param request
     * @param response
     * @param coupon 优惠券信息 {@link Coupon}
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/add")
    public View<Coupon> add(HttpServletRequest request, HttpServletResponse response,
            @Validated @RequestBody Coupon coupon, BindingResult result) {
        View<Coupon> view = new View<Coupon>(20000, "添加成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            String code = null;
            coupon.setCode(code);
            this.couponService.addCoupon(coupon);
        }
        return view;
    }

    /**
     * 更新优惠券
     * @param request
     * @param response
     * @param coupon 优惠券信息 {@link Coupon}
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/update")
    public View<Coupon> update(HttpServletRequest request,
            HttpServletResponse response,
            @Validated @RequestBody Coupon coupon, BindingResult result) {
        View<Coupon> view = new View<Coupon>(20000, "更新成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            Coupon couponDb = this.couponService.selectCouponByCode(coupon.getCode());
            if (couponDb != null) {
                BeanUtils.copy(coupon, couponDb);
                this.couponService.updateCoupon(couponDb);
            }
        }

        return view;
    }

    /**
     * 获取优惠券分页查询列表
     * @param request
     * @param response
     * @param query
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list")
    public View<Page<Coupon>> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<Coupon> query) {
        View<Page<Coupon>> view = new View<Page<Coupon>>(20000, "获取列表成功");

        Coupon coupon = query.getData();

        Page<Coupon> page = new Page<Coupon>(query.getPage(), query.getSize(), couponService.count(coupon));
        
        List<Coupon> couponList = couponService.getCouponList(query.getOffset(), query.getLimit(), coupon);
        
        page.setContent(couponList);
        
        view.setData(page);
        
        return view;
    }

    /**
     * 删除优惠券
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @SuppressWarnings("rawtypes")
    @RequestMapping("/delete/{id}")
    public View delete(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View view = new View(20000, "删除成功");

        Coupon coupon = couponService.selectCoupon(id);
        coupon.setDelFlag(String.valueOf(1));
        this.couponService.updateCoupon(coupon);

        return view;
    }
    
}